<!Doctype html>
<html>

<head>
    <title>oixan的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html" ; charset="utf-8">
    <link rel="stylesheet" type="text/css" href="../css/blog.css">
    <link rel="stylesheet" type="text/css" href="./bcss/swiper.min.css">
</head>

<body>
    <div class="head">
        <div class="headnav">
            <div class="naveframe">
                <ul class="navlist">
                    <li>
                        <a href="../index.html">
                            主页
                        </a>
                    </li>
                    <li>
                        <a href="../bloglist.html">
                            博客
                        </a>
                    </li>
                    <li>
                        <a href="../about.html">
                            关于
                        </a>
                    </li>
                </ul>
            </div>
        </div>

        <div class="headtext">
            <div class="headtext1">世界很简单，人类很复杂。</div>
            <div class="headtext2">浮生若梦，寻寻觅觅。</div>
        </div>
    </div>

    <div class="contents-all">
        <div class="contents">

            <div class="frame">
                <div class="title">
                    <h3>加密解密aes</h3>
                </div>
                <div class="dtime">时间：2018-07-13</div>
                <div class="detail">

                    <br>
                    <textarea id="input" name="input" style="width: 80%" rows="8">这里是要加密的内容!</textarea>
                    <br>key:
                    <input id="inputkey" type='text'> </input>
                    <br>
                    <p>
                    <button id="en" class="btn" onclick="onbtnencrypto()" style="width:100px">加密</button>
                    <button id="de" class="btn" onclick="onbtndecrypto()" style="width:100px">解密</button>
                    </p>
                    <textarea id="output" name="output" style="width: 80%" rows="10"></textarea>


                </div>
                <a href="javascript:void(0)" onclick="showPg()">返回</a>
            </div>


        </div>
    </div>
    <div class="footer">
        <div class="footertext">©2018,oixan博客</div>
    </div>
    <span id="backtop" onclick="topFunction()">返回顶部</span>

    <script src="./bjs/aes.js"></script>

    <script>
        function getByteLen(val) {
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null) len += 3;
        else len += 1;
    }
    return len;
}

function onbtnencrypto() {
    var plaintText = document.getElementById("input").value;
    var keyword = document.getElementById("inputkey").value;
    if (plaintText.replace(/(^\s*)|(\s*$)/g, "") == '') {
        alert("输入要加密的内容！");
        return;
    }
    if (keyword.replace(/(^\s*)|(\s*$)/g, "") == '') {
        alert("输入要加密的key！");
        return;
    }
    while (getByteLen(keyword) % 8 != 0) {
        keyword = keyword + "\0";
    }

    var key = CryptoJS.enc.Utf8.parse(keyword);
    var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    encryptedData = encryptedData.ciphertext.toString();
    document.getElementById("output").value = encryptedData;
}

function onbtndecrypto() {
    var encryptedData = document.getElementById("input").value;
    var keyword = document.getElementById("inputkey").value;
    if (encryptedData.replace(/(^\s*)|(\s*$)/g, "") == '') {
        alert("输入要加密的内容！");
        return;
    }
    if (keyword.replace(/(^\s*)|(\s*$)/g, "") == '') {
        alert("输入要加密的key！");
        return;
    }
    while (getByteLen(keyword) % 8 != 0) {
        keyword = keyword + "\0";
    }

    var key = CryptoJS.enc.Utf8.parse(keyword);
    var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
    var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);

    var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });

    if (decryptedData.sigBytes < 0) {
        document.getElementById("output").value = "解密失败！密文或者key错误!";
        return;
    }
    try {
        decryptedData.toString(CryptoJS.enc.Utf8)
    } catch(e) {
        document.getElementById("output").value = "解密失败！密文或者key错误!";
        return;
    }
    var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    document.getElementById("output").value = decryptedStr;
}
    </script>
</body>
<script  src="../js/fanhuiye.js"></script>

</html>